﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace ExifTool
{
    public class ExifUtil
    {
        /// <summary>
        /// 删除图像的经纬度信息
        /// </summary>
        /// <param name="IN_File">文件路径</param>
        public void DeleteCoord(string IN_File)
        {
            using (Stream ms = new MemoryStream(File.ReadAllBytes(IN_File)))
            {
                Image image = Image.FromStream(ms);
                //构建版本
                byte[] _version = { 2, 2, 0, 0 };
                PRV_SetProperty(image, _version, 0x0000, 1);
                //设置南北半球
                PRV_SetProperty(image, BitConverter.GetBytes('N'), 0x0001, 2);
                //设置纬度
                PRV_SetProperty(image, new byte[0], 0x0002, 5);
                //设置东西半球
                PRV_SetProperty(image, BitConverter.GetBytes('E'), 0x0003, 2);
                //设置经度
                PRV_SetProperty(image, new byte[0], 0x0004, 5);
                //设置高度在海平面上还是下
                byte[] _altref = { 0 };//海平面上
                PRV_SetProperty(image, _altref, 0x0005, 1);
                //设置高度
                PRV_SetProperty(image, new byte[0], 0x0006, 5);
                image.Save(IN_File);
                image.Dispose();
            }



        }
        /// <summary>
        /// 删除图像的经纬度信息
        /// </summary>
        /// <param name="IN_File">文件路径</param>
        public void DeleteCoord(string IN_File, string IN_Save)
        {
            using (Stream ms = new MemoryStream(File.ReadAllBytes(IN_File)))
            {
                Image image = Image.FromStream(ms);
                //构建版本
                byte[] _version = { 2, 2, 0, 0 };
                PRV_SetProperty(image, _version, 0x0000, 1);
                //设置南北半球
                PRV_SetProperty(image, BitConverter.GetBytes('N'), 0x0001, 2);
                //设置纬度
                PRV_SetProperty(image, new byte[0], 0x0002, 5);
                //设置东西半球
                PRV_SetProperty(image, BitConverter.GetBytes('E'), 0x0003, 2);
                //设置经度
                PRV_SetProperty(image, new byte[0], 0x0004, 5);
                //设置高度在海平面上还是下
                byte[] _altref = { 0 };//海平面上
                PRV_SetProperty(image, _altref, 0x0005, 1);
                //设置高度
                PRV_SetProperty(image, new byte[0], 0x0006, 5);
                image.Save(IN_Save);
                image.Dispose();
            }
        }
        /// <summary>
        /// 设置图片参数
        /// </summary>
        /// <param name="IN_Image">图片</param>
        /// <param name="IN_Content">byte[] 要写入的内容</param>
        /// <param name="IN_Id">字段ID</param>
        /// <param name="IN_Type">值类型</param>
        private void PRV_SetProperty(Image IN_Image, byte[] IN_Content, int IN_Id, short IN_Type)
        {
            PropertyItem pi = IN_Image.PropertyItems[0];
            pi.Id = IN_Id;
            pi.Type = IN_Type;
            pi.Value = IN_Content;
            pi.Len = pi.Value.Length;
            IN_Image.SetPropertyItem(pi);
        }
    }
}
